<?php 


class SearchMethod { 
  var $username = "root"; 
  var $password = "24226448hh"; 
  var $server = "localhost"; 
  var $port = "3306"; 
  var $databasename = "elementClass"; 
 
  
  var $connection; 
  public function __construct() { 
    $this->connection = mysqli_connect( 
                       $this->server,  
                       $this->username,  
                       $this->password, 
                       $this->databasename, 
                       $this->port 
                       ); 
    
    $this->throwExceptionOnError($this->connection); 
  } 

  public function getMethods() {
     $stmt = mysqli_prepare($this->connection,
          "SELECT methods.id, methods.name FROM methods");     
         
      $this->throwExceptionOnError();

      mysqli_stmt_execute($stmt);
      $this->throwExceptionOnError();

      $rows = array();
      mysqli_stmt_bind_result($stmt, $row->id, $row->name);

      while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->id, $row->name);
      }

      mysqli_stmt_free_result($stmt);
      mysqli_close($this->connection);

      return $rows;
  }  
   public function getApplicationCharacter() {
     $stmt = mysqli_prepare($this->connection,
          "SELECT ApplicationCharacter.id, 
		  ApplicationCharacter.name, 
		  ApplicationCharacter.description 
		  FROM  ApplicationCharacter");     
         
      $this->throwExceptionOnError();

      mysqli_stmt_execute($stmt);
      $this->throwExceptionOnError();

      $rows = array();
      mysqli_stmt_bind_result($stmt, $row->id, $row->name,  $row->description);

      while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->id, $row->name,  $row->description);
      }

      mysqli_stmt_free_result($stmt);
      mysqli_close($this->connection);

      return $rows;
  }  
  
  public function searchApplicationCharacter($itemID) {
     $stmt = mysqli_prepare($this->connection,
        "CALL searchApplicationCharacter(?)");
		
       $this->throwExceptionOnError();
mysqli_stmt_bind_param($stmt, 'i', $itemID);
      $this->throwExceptionOnError();
      mysqli_stmt_execute($stmt);
      $this->throwExceptionOnError();

      $rows = array();
      mysqli_stmt_bind_result($stmt, $row->id, $row->name,$row->description);

      while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->id, $row->name,$row->description);
      }

      mysqli_stmt_free_result($stmt);
      mysqli_close($this->connection);

      return $rows;

  }  

  
  

  
   public function searchTargetState($itemID) {
          $stmt = mysqli_prepare($this->connection,
        "CALL searchTargetState(?)");
		
      $this->throwExceptionOnError();
          mysqli_stmt_bind_param($stmt, 'i', $itemID);
      $this->throwExceptionOnError();
      mysqli_stmt_execute($stmt);
      $this->throwExceptionOnError();

      $rows = array();
      mysqli_stmt_bind_result($stmt, $row->id, $row->name);

      while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->id, $row->name);
      }

      mysqli_stmt_free_result($stmt);
      mysqli_close($this->connection);

      return $rows;

  }  
  
     public function getTargetState() {
     $stmt = mysqli_prepare($this->connection,
          "SELECT TargetState.id, 
		  TargetState.name
		  FROM  TargetState");     
         
      $this->throwExceptionOnError();

      mysqli_stmt_execute($stmt);
      $this->throwExceptionOnError();

      $rows = array();
      mysqli_stmt_bind_result($stmt, $row->id, $row->name);

      while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->id, $row->name);
      }

      mysqli_stmt_free_result($stmt);
      mysqli_close($this->connection);

      return $rows;
  }  
    
   public function searchCauseCategorie($itemID) {
     $stmt = mysqli_prepare($this->connection,
          "CALL searchCauseCategories(?)");     
   $this->throwExceptionOnError();
    mysqli_stmt_bind_param($stmt, 'i', $itemID);
      $this->throwExceptionOnError();
      mysqli_stmt_execute($stmt);
      $this->throwExceptionOnError();

      $rows = array();
      mysqli_stmt_bind_result($stmt, $row->id, $row->name);

      while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->id, $row->name);
      }

      mysqli_stmt_free_result($stmt);
      mysqli_close($this->connection);

      return $rows;

  }  
  
  public function getCauseCategories() {
     $stmt = mysqli_prepare($this->connection,
          "SELECT CauseCategories.id, 
		  CauseCategories.name
		  FROM  CauseCategories");     
         
      $this->throwExceptionOnError();

      mysqli_stmt_execute($stmt);
      $this->throwExceptionOnError();

      $rows = array();
      mysqli_stmt_bind_result($stmt, $row->id, $row->name);

      while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->id, $row->name);
      }

      mysqli_stmt_free_result($stmt);
      mysqli_close($this->connection);

      return $rows;
  }  
  
   public function searchLevelOfImplementation($itemID) {
     $stmt = mysqli_prepare($this->connection,
          "CALL searchLevelOfImplementation(?)");     
       $this->throwExceptionOnError();
      mysqli_stmt_bind_param($stmt, 'i', $itemID);
      $this->throwExceptionOnError();
      mysqli_stmt_execute($stmt);
      $this->throwExceptionOnError();

      $rows = array();
      mysqli_stmt_bind_result($stmt, $row->id, $row->name);

      while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->id, $row->name);
      }

      mysqli_stmt_free_result($stmt);
      mysqli_close($this->connection);

      return $rows;

  }
  public function getLevelOfImplementation() {
     $stmt = mysqli_prepare($this->connection,
          "SELECT LevelOfImplementation.id, 
		  LevelOfImplementation.name
		  FROM  LevelOfImplementation");     
         
      $this->throwExceptionOnError();

      mysqli_stmt_execute($stmt);
      $this->throwExceptionOnError();

      $rows = array();
      mysqli_stmt_bind_result($stmt, $row->id, $row->name);

      while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->id, $row->name);
      }

      mysqli_stmt_free_result($stmt);
      mysqli_close($this->connection);

      return $rows;
  }  
  
/** 
  * Utitity function to throw an exception if an error occurs 
  * while running a mysql command. 
  */ 
  private function throwExceptionOnError($link = null) { 
    if($link == null) { 
      $link = $this->connection; 
    } 
    if(mysqli_error($link)) { 
      $msg = mysqli_errno($link) . ": " . mysqli_error($link); 
      throw new Exception('MySQL Error - '. $msg); 
    }         
  } 
  
} 
?>
